home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 8020 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  2.3 KB

  1. Path: anvil.ugrad.cs.ubc.ca!not-for-mail
  2. From: c2a192@ugrad.cs.ubc.ca (Kazimir Kylheku)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: why arrays may seem like pointers
  5. Date: 28 Feb 1996 16:05:48 -0800
  6. Organization: Computer Science, University of B.C., Vancouver, B.C., Canada
  7. Message-ID: <4h2qksINN6fl@anvil.ugrad.cs.ubc.ca>
  8. References: <4gsdno$1bg@umbc9.umbc.edu> <313318b8.53776146@nntp.ix.netcom.com> <DnHyrp.CF8@eskimo.com> <3134A1C7.3FB7@oc.com>
  9. NNTP-Posting-Host: anvil.ugrad.cs.ubc.ca
  10.  
  11. In article <3134A1C7.3FB7@oc.com>, Larry Weiss  <lfw@oc.com> wrote:
  12.  >Steve Summit wrote:
  13.  > > 
  14.  > > Having learned this, I'm now careful to say things like, "When
  15.  > > you make reference to an array in an expression, the compiler
  16.  > > automatically generates a pointer to its first element, just as
  17.  > > if you'd written &array[0]."  I try to use words like "generate,"
  18.  > > and *not* to use words like "convert."
  19.  > > 
  20.  >
  21.  >Also, it is common jargon to say the array reference "decays" into
  22.  >a pointer.    The word "decay" confused me when I first learned the
  23.  >language, as it conveys that something actually happened to the array.
  24.  >
  25.  >If you are quite naive, and still pondering strange "algebraic"
  26.  >expressions like  
  27.  >    x = x + 1 
  28.  >then it's even more important to get clear explanations of array
  29.  >semantics.   It's actually "too bad" that one must understand pointers
  30.  >in order to understand arrays, in general, but then, that's C.
  31.  
  32. That is not true. To just learn basic array manipulation, you don't have to be
  33. aware that pointers are involved. The [] postfix operator doesn't readily
  34. betray that it is equivalent to (*(E1 + (E2)). Not until you need to pass an
  35. array into a function, anyway! :)
  36.  
  37. It's possible to conceive of a language that is like C in every regard except
  38. the equivalence of E1[E2] and *(E1 + (E2)), and related equivalences. In this
  39. imaginary language, the only way to get the address of element 0 of an array
  40. is to use &array[0]. An expression consisting of only the name of the
  41. serves as an lvalue for the purpose of array assignment, passing by
  42. value or returning an array from a function.  It is still possible to use []
  43. with pointers: it is an overload operator, the same way you can use + for
  44. pointer arithmetic, integer arithmetic or floating-point arithmetic.
  45. Is there anything in this language that is excessively clumsy to express
  46. compared to using a C idiom?
  47. -- 
  48.  
  49.